<?php
/**
 * @link http://www.anabas.tk/awc/wiki
 *
 * @package Aba Sulution
 * @subpackage Aba_Core
 * @since Aba Core 1.0.1 beta
 */

defined( 'ABSPATH' ) or die(/*Nothing...*/);

defined( 'AWC_CORE' ) or die;
class AWC_Core extends AWC_Menu_Page {
    function __construct(){
        parent::__construct( 'Aba Core Settings', 'Settings', 'administrator', '', 66, 'Aba Core' );
        $this->_ajax_registers();
        $this->_action_registers();
        $this->_filter_registers();
        add_action( 'init', array( $this, '_action_init' ) );
        
        add_action( 'admin_enqueue_scripts', array( $this, '_action_admin_enqueue_scripts' ) );
        
        
        
        $this->id = 'settings';
        $this->method_title = 'AWC Settings';
        $this->method_description = 'You can settings option here.';
        $this->type = '';
        
        
        //awc_reg_init( array( $this, 'admin_options' ), 10, false );
        
        //$this->admin_options();
        
         //add_action( 'plugins_loaded', array( $this, '_plugins_loaded' ) );
         add_action( 'after_setup_theme', array( $this, '_plugins_loaded' ) );
    }
    
    function _ajax_registers(){
        
    }
    
    function _action_registers(){
        
    }
    
    function _filter_registers(){
        if ( AWC_ICR_USE )
            add_filter( 'request', array($this, '_request_filter') , 15, 1 );
        add_filter( 'wp_unique_post_slug', array($this, '_unique_post_slug'), 10, 4 );
        
        add_action('parse_query', function( $query ){
            if ( $query->is_single ){
                $idhash = substr( $query->query['name'], ($posi = strrpos( $query->query['name'],"-", -1 ))!==false?$posi + 1:$posi );
                $query->set( 'p', awc_deid( $idhash ) ); 
                $query->set( 'name', '' );
            }
            //list($uri, $qs) = explode('?', $_SERVER['REQUEST_URI']);
            // Do stuff here:
            // ... STUFF ...
        }, 1, 100); 
    }    
    
    function _unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
        //$post_types = $this->get_option(  );
        //if ( 'p' == $post_type ) {
            //$slug = md5( time() );
        //}
        
        $slug = $slug . '-' . awc_enid( $post_ID );
        
        return $slug;
    }
    
    function _request_filter( $qvars ) {
        $qvars['p'] = awc_deid( $qvars['p'] );
        return apply_filters( '', $qvars );
    }
    
    function _action_init(){
        
    }
    
    function _action_admin_enqueue_scripts(){
        wp_enqueue_style( 'font-awesome', AWC_ASSETS_URI . 'font-awesome/css/font-awesome.min.css' );
        wp_enqueue_style( 'awc-admin-style', AWC_ASSETS_URI . 'css/awc-admin-style.css' );
    }
    
    function _install_extentions(){        
        global $awcore;
        
        if ( empty( $awcore->plugins ) ){
            $awcore->plugins = array();
            return false;
        }        
        
        foreach( $awcore->plugins as $key => $mod ){
            $awcore->plugins[$key]->intance = new $key;
            $awcore->plugins[$key]->active = call_user_func_array( array( $awcore->plugins[$key]->intance, 'trigger' ), array() );
        }
    }
    
    function _plugins_loaded(){
        if ( AWC_AUTOLOAD ) :        
        $all = get_declared_classes();
        foreach( $all as $classname ){
            if( awc_can_pluginload ( $classname ) ){
                 awc_reg_mod( $classname );
            }
        }
        
        define( 'AWC_AUTOLOADED', true );
        
        endif;
        
        do_action( 'awc_plugins_loaded' );
        
        $this->_install_extentions();
    }
    
    function init_form(){
        $this->form_fields = array(
            'name' => array(
                'title' => 'Your name',
                'type' => 'text',
                'placeHolder' => 'Your name',
                'description'   => 'Please, input your name'
            ),
            'age'   => array(
                'title'=>'Your age',
                'type' => 'text',
                'placeHolder' => 'Your age',
                'description'   => 'Please, input your age',
                'desc_tip'   => 'Please, input your age'
            ),
            'use_id_hash'            => array(
                'title'             => __( 'Use ID Hash' ),
                'type'              => 'utien'
            ),
        );
    }
}
?>
